import pandas as pd
import tushare as ts
import os
from sqlalchemy import create_engine
#def use_proxy():
#    HTTP_PROXY = "http_proxy"
#    HTTPS_PROXY = "https_proxy"
#    if (HTTP_PROXY in os.environ):
#        print (os.environ[HTTP_PROXY])
#    else:
#        print ("not set ", HTTP_PROXY)
#        os.environ[HTTP_PROXY] = "127.0.0.1:80"
#    if (HTTPS_PROXY in os.environ):
#        print( os.environ[HTTPS_PROXY])
#    else:
#        print ("not set ", HTTPS_PROXY)
#        os.environ[HTTPS_PROXY] = "127.0.0.1:80"
#
dbpath='/home/xzh/data/stockdata2018.db'
engine= create_engine('sqlite:////'+dbpath)
code='sh'
stdata = pd.read_sql("select * from stocks where date>'2017-01-01' and code='{0}' order by date".format(code),con= engine)
stdata=stdata.sort_index()
stdata['year']=stdata['date'].apply(lambda x:x[0:4])
stdata['date']=stdata['date'].apply(lambda x:int(str(x).replace("-","")))
stdata['xid']=stdata.groupby('year')['date'].rank(ascending=1,method='first')
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig=plt.figure(figsize=(10,4))
ax=fig.add_subplot(1,1,1)
ylist=list(stdata['year'].drop_duplicates())
cname={1:'r',2:'g',3:'b',4:'m',5:'c',6:'y'}
for i in range(len(ylist)):
    stdata[stdata['year']==ylist[i]].plot(x='xid',y='close',ax=ax,c=cname[i+1],legend=False)
plt.legend(ylist)
plt.show()